package com.jimmy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jimmy.reggie.common.R;
import com.jimmy.reggie.entity.Category;
import com.jimmy.reggie.entity.Setmeal;
import com.jimmy.reggie.entity.SetmealDto;
import com.jimmy.reggie.service.CategoryService;
import com.jimmy.reggie.service.SetmealDishService;
import com.jimmy.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
      @Autowired
      private SetmealDishService setmealDishService;
      @Autowired
      private SetmealService setmealService;
      @Autowired
      private CategoryService categoryService;
      @Autowired
      private RedisTemplate<Object,Object> redisTemplate;
      @PostMapping
      public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
          log.info(setmealDto.toString());
          setmealService.saveWithDish(setmealDto);
          return R.success("保存成功");
      }

      @GetMapping("/page")
      public R<Page<SetmealDto>> getPage(int page,int pageSize,String name){
          log.info(page+"\t"+pageSize+"\t"+name);
          Page<Setmeal> setmealPage = new Page<>(page,pageSize);
          Page<SetmealDto> setmealDtoPage =new Page<>();
          LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
          //添加查询条件，根据name进行like模糊查询
          setmealLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
          setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
          setmealService.page(setmealPage,setmealLambdaQueryWrapper);
          BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
          List<SetmealDto> list= setmealPage.getRecords().stream().map((item)->{
              SetmealDto setmealDto = new SetmealDto();
              BeanUtils.copyProperties(item,setmealDto);
              Category category = categoryService.getById(item.getCategoryId());
              setmealDto.setCategoryName(category.getName());
              return setmealDto;
          }).collect(Collectors.toList());
          setmealDtoPage.setRecords(list);
          return R.success(setmealDtoPage);
      }
      @CacheEvict(value = "setmeal",allEntries = true)
      @DeleteMapping
      public R<String> deleteIds(List<Long> ids){
          log.info(String.valueOf(ids));
          setmealService.removeWithDish(ids);
          return R.success("删除成功");
      }

      @GetMapping("/list")
      @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
      public R<List<Setmeal>> list(Setmeal setmeal){
          LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
          setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
          setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
          setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
          List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
          return R.success(list);
      }
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        //不同的表查询，然后封装成dishdto对象进行返回
        SetmealDto setmealDto = setmealService.getByIdWithAll(id);
        if (setmealDto!=null){
            return R.success(setmealDto);
        }
        return R.error("查询不到信息");
    }
    @PutMapping
    public R<String> updateSetmealDto(@RequestBody SetmealDto setmealDto){
        setmealService.UpdateWithDishes(setmealDto);
        String key=setmealDto.getCategoryId()+"_"+setmealDto.getStatus();
        redisTemplate.delete(key);
        return R.success("保存成功");
    }


    @PostMapping("/status/{status}")
    public R<String> updateStatusByIds(@PathVariable Integer status, @RequestParam  List<Long> ids ){
        //将传过来的数据和status，封装成实体类的列表就行了
        List<Setmeal> setmeals = ids.stream().map((item)->{
            Setmeal setmeal = new Setmeal();
            setmeal.setId(item);
            setmeal.setStatus(status);
            return setmeal;
        }).collect(Collectors.toList());
        boolean b = setmealService.updateBatchById(setmeals);
        if (b){
            return R.success("修改成功");
        }
        return R.error("未知错误");
    }

}
